package handlers

import (
	"log"
	"net/http"
	"ssr/common"
	"ssr/dto"
	service "ssr/services"

	"github.com/gin-gonic/gin"
)

type JackpotHandler struct {
	jackpotService *service.JackpotService
}

func NewJackpotHandler() *JackpotHandler {
	return &JackpotHandler{jackpotService: service.NewJackpotService()}
}

func (h *JackpotHandler) GetJackpotUserInfo(c *gin.Context) {
	currentUser := c.MustGet("currentUser").(*common.Claims)
	result, err := h.jackpotService.GetJackpotUserInfo(currentUser.Sub)
	if err != nil {
		c.Set("already_sent", true)
		common.Fail(http.StatusBadRequest, err.Error()).Send(c)
		return
	}
	common.Success(result).Send(c)
}

func (h *JackpotHandler) GetJackpotUserList(c *gin.Context) {
	var queryParams dto.CommonQuery
	if err := c.ShouldBindJSON(&queryParams); err != nil {
		c.Set("already_sent", true)
		common.Fail(http.StatusBadRequest, "Invalid request parameters").Send(c)
		return
	}
	result, err := h.jackpotService.GetJackpotUserList(queryParams)
	if err != nil {
		c.Set("already_sent", true)
		common.Fail(http.StatusBadRequest, err.Error()).Send(c)
		return
	}
	common.Success(result).Send(c)
}
func (h *JackpotHandler) DisposeJackpotUser(c *gin.Context) {
	var user dto.JackpotUserRegisterDTO
	if err := c.ShouldBindJSON(&user); err != nil {
		c.Set("already_sent", true)
		log.Println("DisposeJackpotUser", err.Error())
		common.Fail(http.StatusBadRequest, "Invalid request parameters").Send(c)
		return
	}
	result, err := h.jackpotService.DisposeJackpotUser(user)
	if err != nil {
		c.Set("already_sent", true)
		common.Fail(http.StatusBadRequest, err.Error()).Send(c)
		return
	}
	common.Success(result).Send(c)
}

func (h *JackpotHandler) DeleteJackpotUser(c *gin.Context) {
	var user dto.UserParams
	if err := c.ShouldBindBodyWithJSON(&user); err != nil {
		c.Set("already_sent", true)
		common.Fail(http.StatusBadRequest, "Invalid request parameters").Send(c)
		return
	}
	result, err := h.jackpotService.DeleteJackpotUser(user.UserId)
	if err != nil {
		c.Set("already_sent", true)
		common.Fail(http.StatusBadRequest, err.Error()).Send(c)
		return
	}
	common.Success(result).Send(c)
}

func (h *JackpotHandler) GetGameResultList(c *gin.Context) {
	var queryCommon dto.CommonQuery
	if err := c.ShouldBindJSON(&queryCommon); err != nil {
		c.Set("already_sent", true)
		common.Fail(http.StatusBadRequest, "Invalid request parameters").Send(c)
		return
	}
	if queryCommon.Page == 0 {
		queryCommon.Page = 1
	}
	result, err := h.jackpotService.GetGameResultList(queryCommon)
	if err != nil {
		c.Set("already_sent", true)
		common.Fail(http.StatusBadRequest, err.Error()).Send(c)
		return
	}
	common.Success(result).Send(c)
}

func (h *JackpotHandler) AddGameResult(c *gin.Context) {
	var gameResult dto.GameResultDTO
	if err := c.ShouldBindJSON(&gameResult); err != nil {
		c.Set("already_sent", true)
		log.Println("AddGameResult", err.Error())
		common.Fail(http.StatusBadRequest, "Invalid request parameters").Send(c)
		return
	}
	result, err := h.jackpotService.AddGameResult(gameResult)
	if err != nil {
		c.Set("already_sent", true)
		common.Fail(http.StatusBadRequest, err.Error()).Send(c)
		return
	}
	common.Success(result).Send(c)
}

func (h *JackpotHandler) Recharge(c *gin.Context) {
	var params dto.RechargeParamsDTO
	if err := c.ShouldBindJSON(&params); err != nil {
		c.Set("already_sent", true)
		log.Println("Recharge", err.Error())
		common.Fail(http.StatusBadRequest, "Invalid request parameters").Send(c)
		return
	}
	result, err := h.jackpotService.Recharge(params)
	if err != nil {
		c.Set("already_sent", true)
		common.Fail(http.StatusBadRequest, err.Error()).Send(c)
		return
	}
	common.Success(result).Send(c)
}

func (h *JackpotHandler) OpenGameResult(c *gin.Context) {
	var params dto.OpenGameResultParamsDTO
	if err := c.ShouldBindJSON(&params); err != nil {
		c.Set("already_sent", true)
		log.Println("OpenGameResult", err.Error())
		common.Fail(http.StatusBadRequest, "Invalid request parameters").Send(c)
		return
	}
	result, err := h.jackpotService.OpenGameResult(params)
	if err != nil {
		c.Set("already_sent", true)
		common.Fail(http.StatusBadRequest, err.Error()).Send(c)
		return
	}
	common.Success(result).Send(c)
}

func (h *JackpotHandler) GetOddRuleList(c *gin.Context) {
	var queryCommon dto.OddsQueryDTO
	if err := c.ShouldBindJSON(&queryCommon); err != nil {
		c.Set("already_sent", true)
		common.Fail(http.StatusBadRequest, "Invalid request parameters").Send(c)
		return
	}
	result, err := h.jackpotService.GetOddRuleList(queryCommon)
	if err != nil {
		c.Set("already_sent", true)
		common.Fail(http.StatusBadRequest, err.Error()).Send(c)
		return
	}
	common.Success(result).Send(c)
}

func (h *JackpotHandler) DisposeOddRule(c *gin.Context) {
	var params dto.DisposeOddRuleParamsDTO
	if err := c.ShouldBindJSON(&params); err != nil {
		c.Set("already_sent", true)
		log.Println("DisposeOddRule", err.Error())
		common.Fail(http.StatusBadRequest, "Invalid request parameters").Send(c)
		return
	}
	result, err := h.jackpotService.DisposeOddRule(params)
	if err != nil {
		c.Set("already_sent", true)
		common.Fail(http.StatusBadRequest, err.Error()).Send(c)
		return
	}
	common.Success(result).Send(c)
}

func (h *JackpotHandler) DeleteOddRule(c *gin.Context) {
	type DeleteOddRuleParamsDTO struct {
		RuleId string `json:"ruleId" binding:"required"`
	}
	var params DeleteOddRuleParamsDTO
	if err := c.ShouldBindJSON(&params); err != nil {
		c.Set("already_sent", true)
		log.Println("DeleteOddRule", err.Error())
		common.Fail(http.StatusBadRequest, "Invalid request parameters").Send(c)
		return
	}
	result, err := h.jackpotService.DeleteOddRule(params.RuleId)
	if err != nil {
		c.Set("already_sent", true)
		common.Fail(http.StatusBadRequest, err.Error()).Send(c)
		return
	}
	common.Success(result).Send(c)
}

func (h *JackpotHandler) Bet(c *gin.Context) {
	var params dto.BetFormDTO
	if err := c.ShouldBindJSON(&params); err != nil {
		c.Set("already_sent", true)
		log.Println("Bet", err.Error())
		common.Fail(http.StatusBadRequest, "Invalid request parameters").Send(c)
		return
	}
	result, err := h.jackpotService.Bet(params)
	if err != nil {
		c.Set("already_sent", true)
		common.Fail(http.StatusBadRequest, err.Error()).Send(c)
		return
	}
	common.Success(result).Send(c)
}
